<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>物联网毕设-智能鱼缸 | 冰泉冷涩弦凝绝，凝绝不通声暂歇</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="主要记录项目开发过程遇到的bug，项目进度、以及各个功能模块的实现  @TOC  需求关键字：物联网、网页与硬件交互、传感器数据上传、配网 智能鱼缸实现Wifi联网、网页后台控制、传感器数据上传、基本的鱼缸DIY搭建  提示：以下是本篇文章正文内容，下面案例可供参考 一、主要硬件模块1、原理图：(1)、温度传感器（18b20）    (2)、继电器原理图 (3)、NODE MCU 、8266模块">
<meta property="og:type" content="article">
<meta property="og:title" content="物联网毕设-智能鱼缸">
<meta property="og:url" content="https://gitee.com/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/index.html">
<meta property="og:site_name" content="冰泉冷涩弦凝绝，凝绝不通声暂歇">
<meta property="og:description" content="主要记录项目开发过程遇到的bug，项目进度、以及各个功能模块的实现  @TOC  需求关键字：物联网、网页与硬件交互、传感器数据上传、配网 智能鱼缸实现Wifi联网、网页后台控制、传感器数据上传、基本的鱼缸DIY搭建  提示：以下是本篇文章正文内容，下面案例可供参考 一、主要硬件模块1、原理图：(1)、温度传感器（18b20）    (2)、继电器原理图 (3)、NODE MCU 、8266模块">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://img-blog.csdnimg.cn/562fb2f395124a59a2ff006681ca7353.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_17,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/55ea9131f27f4224a43efb515548d847.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/410153c7ad494e15a659db962e3a4782.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4688592183c04c3bbac25fdc241e18c1.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d5f9b4535f38436090d51331a3565f6c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_13,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2dc4a00e62c945ea8b6c1c92b977658c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_19,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fb8a47921f0f4df8be1af3e11b5a8b58.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/5994104be0e3466b9caf6ddb0eca7795.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/28d2c4c99ee941abb3c949bf9b582055.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ac7e33a816e94f41923e88496d887416.png">
<meta property="article:published_time" content="2022-09-25T07:54:02.000Z">
<meta property="article:modified_time" content="2022-09-25T07:54:49.067Z">
<meta property="article:author" content="lxp">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/562fb2f395124a59a2ff006681ca7353.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_17,color_FFFFFF,t_70,g_se,x_16">
  
    <link rel="alternate" href="/xinhard/atom.xml" title="冰泉冷涩弦凝绝，凝绝不通声暂歇" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/xinhard/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/xinhard/css/style.css">

  
    
<link rel="stylesheet" href="/xinhard/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/xinhard/" id="logo">冰泉冷涩弦凝绝，凝绝不通声暂歇</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/xinhard/">Home</a>
        
          <a class="main-nav-link" href="/xinhard/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/xinhard/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/xinhard"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-物联网毕设-智能鱼缸" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/" class="article-date">
  <time class="dt-published" datetime="2022-09-25T07:54:02.000Z" itemprop="datePublished">2022-09-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      物联网毕设-智能鱼缸
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <blockquote>
<p>主要记录项目开发过程遇到的bug，项目进度、以及各个功能模块的实现</p>
</blockquote>
<p>@<a href="%E7%89%A9%E8%81%94%E7%BD%91%E9%B1%BC%E7%BC%B8%E6%AF%95%E8%AE%BE">TOC</a></p>
<hr>
<h1 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h1><p><code>关键字：物联网、网页与硬件交互、传感器数据上传、配网</code></p>
<p>智能鱼缸实现Wifi联网、网页后台控制、传感器数据上传、基本的鱼缸DIY搭建</p>
<hr>
<p><code>提示：以下是本篇文章正文内容，下面案例可供参考</code></p>
<h1 id="一、主要硬件模块"><a href="#一、主要硬件模块" class="headerlink" title="一、主要硬件模块"></a>一、主要硬件模块</h1><h2 id="1、原理图："><a href="#1、原理图：" class="headerlink" title="1、原理图："></a>1、原理图：</h2><h3 id="1-、温度传感器（18b20）"><a href="#1-、温度传感器（18b20）" class="headerlink" title="(1)、温度传感器（18b20）"></a>(1)、温度传感器（18b20）</h3><p>   <img src="https://img-blog.csdnimg.cn/562fb2f395124a59a2ff006681ca7353.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_17,color_FFFFFF,t_70,g_se,x_16" alt="温度传感器18b20"></p>
<h3 id="2-、继电器"><a href="#2-、继电器" class="headerlink" title="(2)、继电器"></a>(2)、继电器</h3><p>原理图<br><img src="https://img-blog.csdnimg.cn/55ea9131f27f4224a43efb515548d847.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/410153c7ad494e15a659db962e3a4782.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h3 id="3-、NODE-MCU-、8266模块及自动下载电路、串口通信"><a href="#3-、NODE-MCU-、8266模块及自动下载电路、串口通信" class="headerlink" title="(3)、NODE MCU 、8266模块及自动下载电路、串口通信"></a>(3)、NODE MCU 、8266模块及自动下载电路、串口通信</h3><p>8266模块<br><img src="https://img-blog.csdnimg.cn/4688592183c04c3bbac25fdc241e18c1.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>自动下载电路<br><img src="https://img-blog.csdnimg.cn/d5f9b4535f38436090d51331a3565f6c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_13,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>使用usb接口供电兼串口下载&#x2F;串口通信<br><img src="https://img-blog.csdnimg.cn/2dc4a00e62c945ea8b6c1c92b977658c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_19,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>板载外围的电路：<br><img src="https://img-blog.csdnimg.cn/fb8a47921f0f4df8be1af3e11b5a8b58.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>其它版本的NodeMCU板子  PCB图、和本次使用100%兼容<br><img src="https://img-blog.csdnimg.cn/5994104be0e3466b9caf6ddb0eca7795.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h3 id="部分驱动代码："><a href="#部分驱动代码：" class="headerlink" title="部分驱动代码："></a>部分驱动代码：</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Arduino.h&gt;</span></span></span><br><span class="line"><span class="comment">//单总线18b20驱动库</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;OneWire.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;DallasTemperature.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ONE_WIRE_BUS 13<span class="comment">//8266连接GPIO13即D7，Arduino连接port 2</span></span></span><br><span class="line"><span class="comment">// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)</span></span><br><span class="line"><span class="function">OneWire <span class="title">oneWire</span><span class="params">(ONE_WIRE_BUS)</span></span>;</span><br><span class="line"> </span><br><span class="line"><span class="comment">// Pass our oneWire reference to Dallas Temperature. </span></span><br><span class="line"><span class="function">DallasTemperature <span class="title">sensors</span><span class="params">(&amp;oneWire)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  Serial.<span class="built_in">begin</span>(<span class="number">9600</span>);</span><br><span class="line">  Serial.<span class="built_in">println</span>(<span class="string">&quot;Dallas Temperature IC Control Library Demo&quot;</span>);</span><br><span class="line">  <span class="comment">// Start up the library</span></span><br><span class="line">  sensors.<span class="built_in">begin</span>();</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123; </span><br><span class="line">  <span class="comment">// call sensors.requestTemperatures() to issue a global temperature </span></span><br><span class="line">  <span class="comment">// request to all devices on the bus</span></span><br><span class="line">  Serial.<span class="built_in">print</span>(<span class="string">&quot;Requesting temperatures...&quot;</span>);</span><br><span class="line">  sensors.<span class="built_in">requestTemperatures</span>(); <span class="comment">// Send the command to get temperatures</span></span><br><span class="line">  Serial.<span class="built_in">println</span>(<span class="string">&quot;DONE&quot;</span>);</span><br><span class="line">  <span class="comment">// After we got the temperatures, we can print them here.</span></span><br><span class="line">  <span class="comment">// We use the function ByIndex, and as an example get the temperature from the first sensor only.</span></span><br><span class="line">  <span class="type">float</span> tempC = sensors.<span class="built_in">getTempCByIndex</span>(<span class="number">0</span>);</span><br><span class="line">  <span class="comment">// Check if reading was successful</span></span><br><span class="line">  <span class="keyword">if</span>(tempC != DEVICE_DISCONNECTED_C) </span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot;Temperature for the device 1 (index 0) is: &quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(tempC);</span><br><span class="line">  &#125; </span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;Error: Could not read temperature data&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="效果"><a href="#效果" class="headerlink" title="效果"></a>效果</h3><p><img src="https://img-blog.csdnimg.cn/28d2c4c99ee941abb3c949bf9b582055.png" alt="测温"></p>
<h3 id="与网页交换数据"><a href="#与网页交换数据" class="headerlink" title="与网页交换数据"></a>与网页交换数据</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">else</span> <span class="keyword">if</span> (parm == <span class="string">&quot;wd&quot;</span>) <span class="comment">//温度</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">//更新temptrue数据</span></span><br><span class="line">  <span class="built_in">read_temperature</span>();</span><br><span class="line">  esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="built_in">String</span>(temperature));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="网页端请求数据"><a href="#网页端请求数据" class="headerlink" title="网页端请求数据"></a>网页端请求数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">wendu</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> xmlhttp;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">XMLHttpRequest</span>) &#123;</span><br><span class="line">        xmlhttp = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    xmlhttp.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (xmlhttp.<span class="property">status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">            <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;wd&quot;</span>).<span class="property">innerHTML</span> = xmlhttp.<span class="property">responseText</span> + <span class="string">&quot;℃&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    xmlhttp.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>, <span class="string">&quot;/sjhq?sjhq=&quot;</span> + <span class="string">&quot;wd&quot;</span>);</span><br><span class="line">    xmlhttp.<span class="title function_">send</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="2、主要外围设备连接"><a href="#2、主要外围设备连接" class="headerlink" title="2、主要外围设备连接"></a>2、主要外围设备连接</h2><p>因为使用的都是模块，所以就懒得画原理图、弄封装了。这里直接记录8266端口与外围设备的接线：</p>
<table>
<thead>
<tr>
<th>NodeMcu端口</th>
<th>对应模块接线</th>
</tr>
</thead>
<tbody><tr>
<td>D0</td>
<td>喂食继电器</td>
</tr>
<tr>
<td>D1</td>
<td>抽水电机继电器</td>
</tr>
<tr>
<td>D2</td>
<td>增氧继电器</td>
</tr>
<tr>
<td>D5</td>
<td>0.96 OLED SCL</td>
</tr>
<tr>
<td>D6</td>
<td>0.96 OLED SDA</td>
</tr>
<tr>
<td>D7（8266的GPIO13）</td>
<td>18B20数据端 （5.1K上拉电阻）</td>
</tr>
<tr>
<td>D8</td>
<td>灯光控制</td>
</tr>
</tbody></table>
<p>继电器使用5v电源，光耦隔离，高电平有效。OLED使用3.3V供电。由于IO口不够使用，使用D0引脚，D0是MCU睡眠唤醒引脚，8266上电时默认会有一个跳变信号，所以继电器会动作一下，8266本来实际有效引脚就很少。</p>
<h3 id="供电"><a href="#供电" class="headerlink" title="供电"></a>供电</h3><pre><code>推荐使用USB手机插头或者充电宝供电，不推荐使用8266板载micro usb供电
</code></pre>
<h1 id="二、网页端"><a href="#二、网页端" class="headerlink" title="二、网页端"></a>二、网页端</h1><h2 id="1-WIFI配网"><a href="#1-WIFI配网" class="headerlink" title="1.WIFI配网"></a>1.WIFI配网</h2><p>主要实现步骤：ESP8266先运行在AP模式，手机或者电脑连接热点后，弹出强制门户网站（WiFi配网页面），点击网页上配网按钮，即可扫描周围全部WIFI热点，点击热点名称即可把信息输入SSID文本框，用户只需用输入密码即可。最后点击连接WIFI按钮，ESP8266收到网页的SSID、Password信息后，连接此WIFI，如果连接成功，自动跳转到主页面否则重新刷新该页面，网页上显示密码错误。</p>
<h3 id="写入网页文件地址"><a href="#写入网页文件地址" class="headerlink" title="写入网页文件地址"></a>写入网页文件地址</h3><p>使plantform io上传<br><img src="https://img-blog.csdnimg.cn/ac7e33a816e94f41923e88496d887416.png" alt="在这里插入图片描述"></p>
<h3 id="WIFI-配置部分后台代码"><a href="#WIFI-配置部分后台代码" class="headerlink" title="WIFI 配置部分后台代码"></a>WIFI 配置部分后台代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">else</span></span><br><span class="line"> &#123;                          <span class="comment">//未配置过WIFI</span></span><br><span class="line">   <span class="keyword">if</span> (wifiStatus == <span class="literal">false</span>) <span class="comment">//未连接</span></span><br><span class="line">   &#123;</span><br><span class="line">     <span class="comment">// digitalWrite(2, LOW);</span></span><br><span class="line">     WiFi_Status = <span class="string">&quot;0&quot;</span>;</span><br><span class="line">     EEPROM.<span class="built_in">write</span>(WiFi_State_Addr, <span class="number">0</span>);</span><br><span class="line">     EEPROM.<span class="built_in">commit</span>();</span><br><span class="line"></span><br><span class="line">     WiFi.<span class="built_in">disconnect</span>(<span class="literal">true</span>); <span class="comment">//断开连接</span></span><br><span class="line">     Serial.<span class="built_in">println</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">     Serial.<span class="built_in">print</span>(<span class="string">&quot;Start WiFi config \n softAP starting --&gt; &quot;</span>);</span><br><span class="line">     Serial.<span class="built_in">println</span>(AP_name);</span><br><span class="line">     Serial.<span class="built_in">print</span>(<span class="string">&quot;http://&quot;</span>);</span><br><span class="line">     Serial.<span class="built_in">println</span>(url);</span><br><span class="line"></span><br><span class="line">     WiFi.<span class="built_in">mode</span>(WIFI_AP_STA); <span class="comment">//配置为热点模式</span></span><br><span class="line">     WiFi.<span class="built_in">softAPConfig</span>(apIP, apIP, <span class="built_in">IPAddress</span>(<span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">0</span>));</span><br><span class="line"></span><br><span class="line">     WiFi.<span class="built_in">softAP</span>(AP_name); <span class="comment">//热点地址</span></span><br><span class="line">   &#125;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="扫描WIFI-并处理成json"><a href="#扫描WIFI-并处理成json" class="headerlink" title="扫描WIFI 并处理成json"></a>扫描WIFI 并处理成json</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取WiFi加密类型并返回</span></span><br><span class="line"><span class="function">String <span class="title">wifi_type</span><span class="params">(<span class="type">int</span> typecode)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_NONE)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;Open&quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_WEP)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WEP &quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_TKIP)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WPA &quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_CCMP)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WPA2&quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_AUTO)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WPA*&quot;</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">&quot;NONE&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//扫描WiFi，返回json</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">wifiScan</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  String req_json = <span class="string">&quot;&quot;</span>;</span><br><span class="line">  Serial.<span class="built_in">println</span>(<span class="string">&quot;Scan WiFi&quot;</span>);</span><br><span class="line">  <span class="type">int</span> n = WiFi.<span class="built_in">scanNetworks</span>();</span><br><span class="line">  <span class="keyword">if</span> (n &gt; <span class="number">0</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="type">int</span> m = <span class="number">0</span>;</span><br><span class="line">    req_json = <span class="string">&quot;&#123;\&quot;req\&quot;:[&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="keyword">if</span> ((<span class="type">int</span>)WiFi.<span class="built_in">RSSI</span>(i) &gt;= Signal_filtering)</span><br><span class="line">      &#123;</span><br><span class="line">        m++;</span><br><span class="line">        req_json += <span class="string">&quot;&#123;\&quot;ssid\&quot;:\&quot;&quot;</span> + (String)WiFi.<span class="built_in">SSID</span>(i) + <span class="string">&quot;\&quot;,&quot;</span> + <span class="string">&quot;\&quot;encryptionType\&quot;:\&quot;&quot;</span> + <span class="built_in">wifi_type</span>(WiFi.<span class="built_in">encryptionType</span>(i)) + <span class="string">&quot;\&quot;,&quot;</span> + <span class="string">&quot;\&quot;rssi\&quot;:&quot;</span> + (<span class="type">int</span>)WiFi.<span class="built_in">RSSI</span>(i) + <span class="string">&quot;&#125;,&quot;</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    req_json.<span class="built_in">remove</span>(req_json.<span class="built_in">length</span>() - <span class="number">1</span>);</span><br><span class="line">    req_json += <span class="string">&quot;]&#125;&quot;</span>;</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/json;charset=UTF-8&quot;</span>, req_json);</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot;Found &quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">print</span>(m);</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot; WiFi!  &gt;&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">print</span>(Signal_filtering);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;dB&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;scan all wifi :&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(req_json);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="WIFI配置处理部分"><a href="#WIFI配置处理部分" class="headerlink" title="WIFI配置处理部分"></a>WIFI配置处理部分</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//用于WiFi配置，接收ssid、password两个参数</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">wifiConfig</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="comment">// 判断请求是否存在这两个参数以及是否未配置</span></span><br><span class="line">  <span class="keyword">if</span> (esp8266_server.<span class="built_in">hasArg</span>(<span class="string">&quot;ssid&quot;</span>) &amp;&amp; esp8266_server.<span class="built_in">hasArg</span>(<span class="string">&quot;password&quot;</span>) &amp;&amp; WiFi_Status == <span class="string">&quot;0&quot;</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="type">int</span> ssid_len = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;ssid&quot;</span>).<span class="built_in">length</span>();</span><br><span class="line">    <span class="type">int</span> password_len = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;password&quot;</span>).<span class="built_in">length</span>();</span><br><span class="line">    <span class="comment">// 判断ssid和密码长度是否合理</span></span><br><span class="line">    <span class="keyword">if</span> ((ssid_len &gt; <span class="number">0</span>) &amp;&amp; (ssid_len &lt; <span class="number">33</span>) &amp;&amp; (password_len &gt; <span class="number">7</span>) &amp;&amp; (password_len &lt; <span class="number">65</span>))</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="comment">// 把参数获得的String赋值到ssid</span></span><br><span class="line">      String ssid_str = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;ssid&quot;</span>);</span><br><span class="line">      String password_str = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;password&quot;</span>);</span><br><span class="line">      <span class="type">const</span> <span class="type">char</span> *ssid = ssid_str.<span class="built_in">c_str</span>();</span><br><span class="line">      <span class="type">const</span> <span class="type">char</span> *password = password_str.<span class="built_in">c_str</span>();</span><br><span class="line">      Serial.<span class="built_in">print</span>(<span class="string">&quot;SSID: &quot;</span>);</span><br><span class="line">      Serial.<span class="built_in">println</span>(ssid);</span><br><span class="line">      Serial.<span class="built_in">print</span>(<span class="string">&quot;Password: &quot;</span>);</span><br><span class="line">      Serial.<span class="built_in">println</span>(password);</span><br><span class="line">      <span class="comment">// 开始连接WiFi</span></span><br><span class="line">      WiFi.<span class="built_in">begin</span>(ssid, password);</span><br><span class="line">      Serial.<span class="built_in">print</span>(<span class="string">&quot;Connenting&quot;</span>);</span><br><span class="line">      <span class="comment">//判断是否连接成功，8秒超时</span></span><br><span class="line">      <span class="type">unsigned</span> <span class="type">long</span> millis_time = <span class="built_in">millis</span>();</span><br><span class="line">      <span class="keyword">while</span> ((WiFi.<span class="built_in">status</span>() != WL_CONNECTED) &amp;&amp; (<span class="built_in">millis</span>() - millis_time &lt; <span class="number">8000</span>))</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">        Serial.<span class="built_in">print</span>(<span class="string">&quot;.&quot;</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 判断是否连接成功</span></span><br><span class="line">      <span class="keyword">if</span> (WiFi.<span class="built_in">status</span>() == WL_CONNECTED)</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="comment">// 连接成功，返回1</span></span><br><span class="line">        <span class="built_in">digitalWrite</span>(<span class="number">2</span>, HIGH);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;Connected successfully!&quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">print</span>(<span class="string">&quot;IP Address: &quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">println</span>(WiFi.<span class="built_in">localIP</span>());</span><br><span class="line">        Serial.<span class="built_in">print</span>(<span class="string">&quot;http://&quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">println</span>(Hostname);</span><br><span class="line">        esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;1&quot;</span>);</span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">300</span>);</span><br><span class="line">        WiFi_Status = <span class="string">&quot;1&quot;</span>;</span><br><span class="line">        <span class="comment">// 记录状态到EEPROM：已配置</span></span><br><span class="line">        EEPROM.<span class="built_in">write</span>(WiFi_State_Addr, <span class="string">&#x27;1&#x27;</span>);</span><br><span class="line">        EEPROM.<span class="built_in">commit</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//存储WIFI SSID、Password到EEPROM中</span></span><br><span class="line">        <span class="built_in">writeString</span>(<span class="number">10</span>, ssid_str, password_str);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;save ok&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">50</span>);</span><br><span class="line">        <span class="comment">//关闭热点</span></span><br><span class="line">        WiFi.<span class="built_in">softAPdisconnect</span>();</span><br><span class="line"></span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">2000</span>);</span><br><span class="line">        ESP.<span class="built_in">restart</span>();</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span></span><br><span class="line">      &#123;</span><br><span class="line">        esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;0&quot;</span>);</span><br><span class="line">        <span class="comment">// 连接失败, 返回0</span></span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;Connenting failed!&quot;</span>);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">      <span class="comment">// WiFi密码格式错误</span></span><br><span class="line">      Serial.<span class="built_in">println</span>(<span class="string">&quot;Password format error&quot;</span>);</span><br><span class="line">      esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;Password format error&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// 请求参数错误</span></span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;Request parameter error&quot;</span>);</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;Request parameter error&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="WIFI-配置部分网页端"><a href="#WIFI-配置部分网页端" class="headerlink" title="WIFI 配置部分网页端"></a>WIFI 配置部分网页端</h3><p>网页端使用html显示，点击扫描按钮，发送处理请求到ESP8266端，ESP8266调用函数，处理成json数据返回给html，使用<strong>Ajax</strong>接收数据，接收完毕后会显示所有WIFI列表，用户点击某个WIFI名称后输入密码，此时<strong>Ajax</strong>返回数据给ESP8266，8266开始调用连接WIFI函数，无论是否连接成功都会返回给网页端数据给html页面上，给用户显示，成功后8266会重启。</p>
<h3 id="存储WIFI-SSID、PassWord到EEPROM中"><a href="#存储WIFI-SSID、PassWord到EEPROM中" class="headerlink" title="存储WIFI SSID、PassWord到EEPROM中"></a>存储WIFI SSID、PassWord到EEPROM中</h3><p>这两个普通操作函数我写了大几个小时、可能那天状态不是很好，各方面测试都试过、包括中文也可以存储到EEPROM中。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*@brief：把SSID 、Password写入EEPROM中</span></span><br><span class="line"><span class="comment">*地址8：ssid_str 长度 地址9：pws_str长度，</span></span><br><span class="line"><span class="comment">*从地址10开始为ssid_str、pws_str连续存储内容</span></span><br><span class="line"><span class="comment">*@return: psw_start 密码地址起始位置</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">uint8_t</span> <span class="title">writeString</span><span class="params">(<span class="type">int</span> start,String ssid_str,String psw)</span></span>&#123;</span><br><span class="line">  </span><br><span class="line">      <span class="comment">//String ssid_str = &quot;micro&quot;;</span></span><br><span class="line">      <span class="comment">//String psw = &quot;arduino01&quot;;</span></span><br><span class="line"></span><br><span class="line">      <span class="type">uint8_t</span> ssid_str_len = ssid_str.<span class="built_in">length</span>();</span><br><span class="line">      <span class="type">uint8_t</span> psw_len = psw.<span class="built_in">length</span>();</span><br><span class="line">      EEPROM.<span class="built_in">write</span>(<span class="number">8</span>,ssid_str_len);</span><br><span class="line">      EEPROM.<span class="built_in">write</span>(<span class="number">9</span>,psw_len);</span><br><span class="line">      EEPROM.<span class="built_in">commit</span>();</span><br><span class="line">      <span class="built_in">delay</span>(<span class="number">30</span>);</span><br><span class="line">      <span class="comment">//写入ssid、psw字符串</span></span><br><span class="line">      <span class="type">int</span> ssid_start_addre;</span><br><span class="line">      <span class="type">int</span> ssid_end_addre;</span><br><span class="line">      <span class="type">int</span> psw_start;</span><br><span class="line">      <span class="type">int</span> psw_end_addre;</span><br><span class="line">      <span class="comment">//读取ssid的长度、psw的长度</span></span><br><span class="line">      <span class="comment">//uint8_t ssid_len = EEPROM.read(8);</span></span><br><span class="line">      <span class="comment">//uint8_t psw_len = EEPROM.read(9);</span></span><br><span class="line">      <span class="comment">//写入ssid_str      </span></span><br><span class="line">      <span class="type">int</span> i;</span><br><span class="line">      <span class="keyword">for</span>(i = start;i &lt; start+ssid_str_len;i++)&#123;</span><br><span class="line">          EEPROM.<span class="built_in">write</span>(i,ssid_str[i-start]);</span><br><span class="line">          <span class="built_in">delay</span>(<span class="number">30</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      ssid_end_addre = i;</span><br><span class="line">      psw_start = i+<span class="number">1</span>;</span><br><span class="line">      EEPROM.<span class="built_in">commit</span>();</span><br><span class="line">      <span class="comment">//写入psw，在ssid_str结束的下一个位置</span></span><br><span class="line">      <span class="keyword">for</span>(i = psw_start;i &lt; psw_start+psw_len; i++)&#123;</span><br><span class="line">        EEPROM.<span class="built_in">write</span>(i,psw[i-psw_start]);</span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">30</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      EEPROM.<span class="built_in">commit</span>();</span><br><span class="line">      psw_end_addre = i;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> psw_start;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*@brief:从地址10开始读取SSID、PAssword</span></span><br><span class="line"><span class="comment">*@Param：uint8_t： flag:0 返回ssid_str  1:返回psw_str</span></span><br><span class="line"><span class="comment">*@return String</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function">String <span class="title">getStringInfo</span><span class="params">(<span class="type">uint8_t</span> flag)</span></span>&#123;</span><br><span class="line">    <span class="comment">//从EEprom中读ssid_str</span></span><br><span class="line">      String temp_str = <span class="string">&quot;&quot;</span>;</span><br><span class="line">      <span class="type">int</span> i,psw_start;</span><br><span class="line">      <span class="type">uint8_t</span> ssid_str_len,pws_str_len;</span><br><span class="line"></span><br><span class="line">      ssid_str_len = EEPROM.<span class="built_in">read</span>(<span class="number">8</span>);</span><br><span class="line">      pws_str_len = EEPROM.<span class="built_in">read</span>(<span class="number">9</span>);</span><br><span class="line">      Serial.<span class="built_in">println</span>(ssid_str_len);</span><br><span class="line">      Serial.<span class="built_in">println</span>(pws_str_len);</span><br><span class="line">      <span class="keyword">if</span> (flag == <span class="number">0</span>) <span class="comment">//ssid_str</span></span><br><span class="line">      &#123;</span><br><span class="line">           <span class="keyword">for</span>(i = <span class="number">10</span>;i &lt; <span class="number">10</span>+ssid_str_len;i++)&#123;</span><br><span class="line">                temp_str += <span class="built_in">char</span>(EEPROM.<span class="built_in">read</span>(i));</span><br><span class="line">           &#125;</span><br><span class="line">           </span><br><span class="line">      &#125;<span class="keyword">else</span> <span class="keyword">if</span>(flag == <span class="number">1</span>)&#123;    <span class="comment">//psw_str</span></span><br><span class="line">            psw_start = <span class="number">10</span>+ssid_str_len+<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">for</span>(i = psw_start;i &lt; pws_str_len+psw_start;i++)&#123;</span><br><span class="line">                temp_str += <span class="built_in">char</span>(EEPROM.<span class="built_in">read</span>(i));</span><br><span class="line">           &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> temp_str;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="2-网页数据显示、控制"><a href="#2-网页数据显示、控制" class="headerlink" title="2.网页数据显示、控制"></a>2.网页数据显示、控制</h2><p>代码如下（示例）：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//处理index所有数据获取请求</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">sjhq</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">   String parm = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;sjhq&quot;</span>);</span><br><span class="line">   <span class="keyword">if</span> (parm == <span class="string">&quot;wd&quot;</span>) <span class="comment">//温度</span></span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;respond wen du request.&quot;</span>);</span><br><span class="line">    <span class="comment">//更新temptrue数据</span></span><br><span class="line">    <span class="built_in">read_temperature</span>();</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="built_in">String</span>(temperature));</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (parm == <span class="string">&quot;light&quot;</span>) <span class="comment">//灯光按钮</span></span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;light clicked...&quot;</span>);</span><br><span class="line">    dg = !dg;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;light:&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(dg?<span class="string">&quot;on&quot;</span>:<span class="string">&quot;off&quot;</span>);</span><br><span class="line"></span><br><span class="line">   <span class="built_in">digitalWrite</span>(Light, dg);</span><br><span class="line"></span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> <span class="keyword">if</span> (parm == <span class="string">&quot;sxhs&quot;</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    sxh = !sxh;</span><br><span class="line">    <span class="built_in">digitalWrite</span>(ShuiXunHuan, sxh);</span><br><span class="line"></span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;水循环&quot;</span>);</span><br><span class="line"></span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(parm == <span class="string">&quot;zy&quot;</span>)&#123;   <span class="comment">//增氧</span></span><br><span class="line">    zeng_yang = !zeng_yang;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">digitalWrite</span>(ZengYang,zeng_yang);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;ZengYang:&quot;</span>+zeng_yang);</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>,<span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line"></span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(parm == <span class="string">&quot;zengyangStatus&quot;</span>)&#123; <span class="comment">//获取增氧开关状态</span></span><br><span class="line">      Serial.<span class="built_in">println</span>(<span class="string">&quot;respond zengyangStatus request.&quot;</span>);</span><br><span class="line">      esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="built_in">String</span>(<span class="built_in">digitalRead</span>(ZengYang)?<span class="number">1</span>:<span class="number">0</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(parm == <span class="string">&quot;timeFeed&quot;</span>)&#123;</span><br><span class="line"></span><br><span class="line">    String value = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;value&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;select timer value:&quot;</span>+value);</span><br><span class="line">    <span class="comment">//电机转3s后停止</span></span><br><span class="line">    <span class="comment">//digitalWrite(ZengYang,HIGH);</span></span><br><span class="line">    <span class="built_in">digitalWrite</span>(Feed,HIGH);</span><br><span class="line">    <span class="built_in">delay</span>(<span class="number">3000</span>);</span><br><span class="line">    <span class="built_in">digitalWrite</span>(Feed,LOW);</span><br><span class="line">    <span class="comment">//digitalWrite(ZengYang,LOW);</span></span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>,<span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3、网页端数据请求"><a href="#3、网页端数据请求" class="headerlink" title="3、网页端数据请求"></a>3、网页端数据请求</h2><p>下面只展示两个处理、其它大同小异</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">wendu</span>(<span class="params"></span>) &#123;</span><br><span class="line">       <span class="keyword">var</span> xmlhttp;</span><br><span class="line">       <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">XMLHttpRequest</span>) &#123;</span><br><span class="line">           xmlhttp = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">if</span> (xmlhttp.<span class="property">status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">               <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;wd&quot;</span>).<span class="property">innerHTML</span> = xmlhttp.<span class="property">responseText</span> + <span class="string">&quot;℃&quot;</span>;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>, <span class="string">&quot;/sjhq?sjhq=&quot;</span> + <span class="string">&quot;wd&quot;</span>);</span><br><span class="line">       xmlhttp.<span class="title function_">send</span>();</span><br><span class="line">   &#125;</span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">shuixunhuan</span>(<span class="params"></span>) &#123;</span><br><span class="line">       <span class="keyword">var</span> xmlhttp;</span><br><span class="line">       <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">XMLHttpRequest</span>) &#123;</span><br><span class="line">           xmlhttp = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">if</span> (xmlhttp.<span class="property">status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">               <span class="keyword">if</span> (xmlhttp.<span class="property">responseText</span> == <span class="string">&quot;1&quot;</span>) &#123;</span><br><span class="line"></span><br><span class="line">                   <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;sxh&quot;</span>).<span class="property">innerHTML</span> = <span class="string">&quot;开启&quot;</span></span><br><span class="line">               &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">                   <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;sxh&quot;</span>).<span class="property">innerHTML</span> = <span class="string">&quot;关闭&quot;</span></span><br><span class="line">                   <span class="comment">//  document.getElementById(&quot;sxh&quot;).innerHTML = xmlhttp.responseText;</span></span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">          </span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>, <span class="string">&quot;/sjhq?sjhq=&quot;</span> + <span class="string">&quot;sxh&quot;</span>);</span><br><span class="line">       xmlhttp.<span class="title function_">send</span>();</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<hr>
<h1 id="项目进度"><a href="#项目进度" class="headerlink" title="项目进度"></a>项目进度</h1><ol>
<li>2022年3月17日<br> 主要完成了18B20温度传感器软硬件的实现，并调试成功。使用了文件系统并上传网页到ESP8266闪存里、Web  服务器</li>
<li>2022年3月18日<br> 今天上课上到下午4点，又排队做核酸检测，头疼。什么都没做</li>
<li>2022年3月19日<br>   今天实现了WIFI配网的移植，主要有Ajax技术，但存储WIFI功能还未实现</li>
<li>2022年3月20日<br>今天实现了WIFI配网成功后把WIFI SSID、Password存储到EEPROM中，重启后自动连接此WIFI。</li>
<li>2022年3月21日<br> 今天上了一天课，啥都没做。</li>
<li>2022年3月22日<br>今天上课到下午6点，晚上把index.html主页内容显示到8266上，并成功显示了温度数据，每3秒更新一次。但8266连接热点后不能域名解析了。</li>
<li>2022年3月23日<br> 今天没做</li>
<li>2022年3月24日<br> 今天没做</li>
<li>2022年3月25日<br>今天没做</li>
<li>2022年3月26日<br>今天上午刚收到快递，快递等的实在是太久了！今天把外围设备都焊接调试好了，只逻辑代码了，还需要解决默认进入后台页面地址问题，明天准备接一个OLED屏幕，把每次的ip地址显示出来，直接使用地址来访问页面。</li>
<li>2022年3月27日<br>今天上午实现了OLED屏幕显示，最开始使用u8glib库，但有bug。后来换成另外一个库成功跑通。下午、晚上实现了OLED上显示系统ip、系统提示基本信息。</li>
<li>2022年3月28日<br>今天总体测试，排除可能存在的bug、优化使用体验。</li>
<li>2022年3月29日<br>今天没做</li>
<li>2022年3月30日<br>今天没做</li>
<li>2022年3月31日<br>晚上最后测试。打包快递。</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/" data-id="clhnlozw700037ku8ag4f31yw" data-title="物联网毕设-智能鱼缸" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/xinhard/2023/01/28/IIC%E5%8D%8F%E8%AE%AE%E5%AD%A6%E4%B9%A0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          IIC协议学习
        
      </div>
    </a>
  
  
    <a href="/xinhard/2022/09/25/%E7%A1%AC%E4%BB%B6%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">硬件项目汇总</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/xinhard/archives/2023/05/">May 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/xinhard/archives/2023/01/">January 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/xinhard/archives/2022/09/">September 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/xinhard/2023/05/14/STM32%E5%8F%AF%E8%B0%83%E7%94%B5%E6%BA%90/">STM32可调电源</a>
          </li>
        
          <li>
            <a href="/xinhard/2023/01/28/IIC%E5%8D%8F%E8%AE%AE%E5%AD%A6%E4%B9%A0/">IIC协议学习</a>
          </li>
        
          <li>
            <a href="/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/">物联网毕设-智能鱼缸</a>
          </li>
        
          <li>
            <a href="/xinhard/2022/09/25/%E7%A1%AC%E4%BB%B6%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/">硬件项目汇总</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 lxp<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/xinhard/" class="mobile-nav-link">Home</a>
  
    <a href="/xinhard/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/xinhard/js/jquery-3.4.1.min.js"></script>



  
<script src="/xinhard/fancybox/jquery.fancybox.min.js"></script>




<script src="/xinhard/js/script.js"></script>





  </div>
</body>
</html>